package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.*;
import com.usian.pojo.*;
import com.usian.service.TBItemService;
import com.usian.utils.IDUtils;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 韩耀东
 * @date 2021/1/5 20:33
 */
@Service
public class TBItemServiceImpl implements TBItemService {

    @Autowired
    private TbItemMapper itemMapper;
    @Autowired
    private TbItemDescMapper itemDescMapper;
    @Autowired
    private TbItemParamItemMapper itemParamItemMapper;
    @Autowired
    private TbItemCatMapper itemCatMapper;
    @Autowired
    private TbItemParamMapper itemParamMapper;

    /***
     * @Author 韩耀东
     * @Description //TODO 测试项目是否搭建成功。根据id查询ITEM
     * @Date 2021/1/6 8:46
     * @Param [id]
     * @return com.usian.pojo.TbItem
     **/
    @Override
    public TbItem selectItemInfo(Long id) {
        return itemMapper.selectByPrimaryKey(id);
    }

    /**
     * @return com.usian.utils.PageResult
     * @Author 韩耀东
     * @Description //TODO 分页查询所有的商品
     * @Date 2021/1/6 8:46
     * @Param [page 数量, rows 大小]
     **/
    @Override
    public PageResult selectTbItemAllByPage(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        TbItemExample itemExample = new TbItemExample();
        //按修改的时间逆序排序
        itemExample.setOrderByClause("updated DESC");
        TbItemExample.Criteria criteria = itemExample.createCriteria();
        //状态要是1
        criteria.andStatusEqualTo((byte) 1);
        List<TbItem> listItem = itemMapper.selectByExample(itemExample);
        for (TbItem tbItem : listItem) {
            //修改每个商品的价格
            tbItem.setPrice(tbItem.getPrice() / 100);
        }
        PageInfo<TbItem> tbItemPageInfo = new PageInfo<>(listItem);
        PageResult pageResult = new PageResult();
        pageResult.setPageIndex(tbItemPageInfo.getPageNum());
        pageResult.setResult(tbItemPageInfo.getList());
        pageResult.setTotalPage((long) tbItemPageInfo.getPrePage());
        return pageResult;
    }

    /**
     * @return com.usian.utils.Result
     * @Author 韩耀东
     * @Description //TODO 添加商品
     * @Date 2021/1/6 11:04
     * @Param [tbItem, desc, itemParams]
     **/
    @Override
    public Integer insertTbItem(TbItem tbItem, String desc, String itemParams) {
        //补齐 Tbitem 数据
        Long itemId = IDUtils.genItemId();
        Date date = new Date();
        tbItem.setId(itemId);
        tbItem.setStatus((byte) 1);
        tbItem.setUpdated(date);
        tbItem.setCreated(date);
        tbItem.setPrice(tbItem.getPrice() * 100);
        Integer tbItemNum = itemMapper.insertSelective(tbItem);

        //补齐商品描述对象
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemId(itemId);
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setCreated(date);
        tbItemDesc.setUpdated(date);
        Integer tbitemDescNum = itemDescMapper.insertSelective(tbItemDesc);

        //补齐商品规格参数
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setItemId(itemId);
        tbItemParamItem.setParamData(itemParams);
        tbItemParamItem.setUpdated(date);
        tbItemParamItem.setCreated(date);
        Integer itemParamItmeNum = itemParamItemMapper.insertSelective(tbItemParamItem);

        return tbItemNum + tbitemDescNum + itemParamItmeNum;
    }

    @Override
    public Map<String, Object> preUpdateItem(Long itemId) {
        Map<String, Object> map = new HashMap<>();
        //得到商品的信息
        TbItem item = itemMapper.selectByPrimaryKey(itemId);
        item.setPrice(item.getPrice() / 100);
        map.put("item", item);
        //商品的分类
        TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
        map.put("itemDesc", itemDesc);
        //商品的描述
        TbItemCat itemCat = itemCatMapper.selectByPrimaryKey(item.getCid());
        map.put("itemCat", itemCat);
        //商品规格参数
        TbItemParamItemExample itemParamItemExample = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = itemParamItemExample.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> listItemParamItem = itemParamItemMapper.selectByExampleWithBLOBs(itemParamItemExample);
        if (listItemParamItem == null || listItemParamItem.size() == 0) {
            return map;
        }
        map.put("itemParamItem", listItemParamItem.get(0).getParamData());
        return map;
    }

    @Override
    public Integer updateTbItem(TbItem tbItem, String desc, String itemParams) {
        //补齐 Tbitem 数据
//        Long itemId = IDUtils.genItemId();
        Date date = new Date();
        TbItem item = new TbItem();
        item.setTitle(tbItem.getTitle());
        item.setSellPoint(tbItem.getSellPoint());
        item.setNum(tbItem.getNum());
        item.setBarcode(tbItem.getBarcode());
        item.setImage(tbItem.getImage());
        item.setCid(tbItem.getCid());
        tbItem.setStatus((byte) 1);
        tbItem.setUpdated(date);
        tbItem.setCreated(date);
        tbItem.setPrice(tbItem.getPrice() * 100);
        tbItem.setId(tbItem.getId());
        Integer tbItemNum = itemMapper.updateByPrimaryKey(tbItem);

        //补齐商品描述对象
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setCreated(date);
        tbItemDesc.setUpdated(date);
        tbItemDesc.setItemId(tbItem.getId());
        Integer tbitemDescNum = itemDescMapper.updateByPrimaryKey(tbItemDesc);

        //补齐商品规格参数
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setParamData(itemParams);
        tbItemParamItem.setUpdated(date);
        tbItemParamItem.setCreated(date);
        TbItemParamItemExample itemParamItemExample = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = itemParamItemExample.createCriteria();
        criteria.andItemIdEqualTo(tbItem.getId());
        List<TbItemParamItem> tbItemParamItems = itemParamItemMapper.selectByExample(itemParamItemExample);
        tbItemParamItem.setId(tbItemParamItems.get(0).getId());
        Integer itemParamItmeNum = itemParamItemMapper.insertSelective(tbItemParamItem);
        return tbItemNum + tbitemDescNum + itemParamItmeNum;
    }

    @Override
    public Integer deleteItemById(Long itemId) {
        //删除商品规格参数
        TbItemParamItemExample itemParamItemExample = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = itemParamItemExample.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> tbItemParamItems = itemParamItemMapper.selectByExample(itemParamItemExample);
        Integer itemParamItem = 0;
        if (tbItemParamItems.size() != 0) {
            itemParamItem = itemParamItemMapper.deleteByPrimaryKey(tbItemParamItems.get(0).getId());
        }
        //删除商品描述对象
        Integer itemDesc = itemDescMapper.deleteByPrimaryKey(itemId);
        //删除 Tbitem 数据
        Integer item = itemMapper.deleteByPrimaryKey(itemId);
        return itemParamItem + itemDesc + item;
    }
}
